﻿// ***********************************************************************
// Assembly         : YERIL_PI_INT
// Author           : Diego Bonilla
// Created          : 03-02-2014
//
// Last Modified By : Diego Bonilla
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="DetalleMaterialNoEstructural.aspx.cs" company="Development Solutions CR">
//     Copyright (c) Development Solutions CR. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// The Login namespace.
/// </summary>
namespace YERIL_PI_INT.PaginasWebs.Modulos.Login
{
    /// <summary>
    /// Class DetalleMaterialNoEstructural.
    /// </summary>
    public partial class DetalleMaterialNoEstructural : System.Web.UI.Page
    {
        /// <summary>
        /// The codigo_orden
        /// </summary>
        static int codigo_orden = 0;
        /// <summary>
        /// The codigo_material
        /// </summary>
        static int codigo_material = 0;
        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Codigo_Orden"] != null)
            {
                codigo_orden = Convert.ToInt32(Session["Codigo_Orden"]);

            }
            if (!IsPostBack)
            {
             
                CargarGrid();
                txtPrecioUnitario.Enabled = false;
                txtTotalCol.Enabled = false;
            }
        }

        /// <summary>
        /// Cargars the grid.
        /// </summary>
        public void CargarGrid()
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Código Material", Type.GetType("System.String"));
            dt.Columns.Add("Material", Type.GetType("System.String"));
            dt.Columns.Add("Descripción", Type.GetType("System.String"));
            dt.Columns.Add("Unidades", Type.GetType("System.String"));
            dt.Columns.Add("Precio Unitario", Type.GetType("System.String"));

            int cont = dt.Columns.Count;
            for (int i = 0; i < ListaMaterialesNoEstructurales().Count; i++)
            {
                dt.Rows.Add();
                dt.Rows[dt.Rows.Count - 1]["Código Material"] = ListaMaterialesNoEstructurales()[i].MATNOEST_codmatnoest;
                dt.Rows[dt.Rows.Count - 1]["Material"] = ListaMaterialesNoEstructurales()[i].MATNOEST_nombre;
                dt.Rows[dt.Rows.Count - 1]["Descripción"] = ListaMaterialesNoEstructurales()[i].MATNOEST_descripcion;
                dt.Rows[dt.Rows.Count - 1]["Unidades"] = ListaMaterialesNoEstructurales()[i].MATNOEST_uninombre;
                dt.Rows[dt.Rows.Count - 1]["Precio Unitario"] = ListaMaterialesNoEstructurales()[i].MATNOEST_preciouni;

            }

            gvMaterialNoEstructural.DataSource = dt;
            gvMaterialNoEstructural.DataBind();

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("Código Linea", Type.GetType("System.String"));

            dt2.Columns.Add("Material", Type.GetType("System.String"));
            dt2.Columns.Add("Descripción", Type.GetType("System.String"));

            dt2.Columns.Add("Cantidad", Type.GetType("System.String"));
            dt2.Columns.Add("Precio Total", Type.GetType("System.String"));


            for (int i = 0; i < listaDetalleNoEstructural().Count; i++)
            {
                dt2.Rows.Add();
                dt2.Rows[dt2.Rows.Count - 1]["Código Linea"] = listaDetalleNoEstructural()[i].DETORDNOEST_codlinea;
                dt2.Rows[dt2.Rows.Count - 1]["Material"] = listaDetalleNoEstructural()[i].DETORDNOEST_material;
                dt2.Rows[dt2.Rows.Count - 1]["Descripción"] = listaDetalleNoEstructural()[i].DETORDNOEST_descripcion;
                dt2.Rows[dt2.Rows.Count - 1]["Cantidad"] = listaDetalleNoEstructural()[i].DETORDNOEST_cantmaterial;
                dt2.Rows[dt2.Rows.Count - 1]["Precio Total"] = listaDetalleNoEstructural()[i].DETORDNOEST_totallineanoest;

            }

           

            gvDetalleNoEstructural.DataSource = dt2;
            gvDetalleNoEstructural.DataBind();

            

           
        }

        /// <summary>
        /// Listas the materiales no estructurales.
        /// </summary>
        /// <returns>List{WSGestion.MOD_Material_No_Estructural}.</returns>
        private List<WSGestion.MOD_Material_No_Estructural> ListaMaterialesNoEstructurales()
        {

            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            List<WSGestion.MOD_Material_No_Estructural> ListaMaterialesNoEstructurales = wsgest.SEL_Grid_Material_No_Estructural().ToList();
            return ListaMaterialesNoEstructurales;

        }

        /// <summary>
        /// Listas the detalle no estructural.
        /// </summary>
        /// <returns>List{WSGestion.MOD_Detalle_No_Estructural}.</returns>
        private List<WSGestion.MOD_Detalle_No_Estructural> listaDetalleNoEstructural()
        {

            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            List<WSGestion.MOD_Detalle_No_Estructural> listaDetalleNoEstructural = wsgest.SEL_Grid_Detalle_No_Estructural(codigo_orden).ToList();
            return listaDetalleNoEstructural;

        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gvMaterialNoEstructural control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void gvMaterialNoEstructural_SelectedIndexChanged(object sender, EventArgs e)
        {
            codigo_material = Convert.ToInt32(gvMaterialNoEstructural.SelectedRow.Cells[1].Text);
            txtMaterial.Text = gvMaterialNoEstructural.SelectedRow.Cells[2].Text;
            txtPrecioUnitario.Text = gvMaterialNoEstructural.SelectedRow.Cells[5].Text;
        }

        /// <summary>
        /// Handles the TextChanged event of the txtCantMat control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void txtCantMat_TextChanged(object sender, EventArgs e)
        {
            double total_col = Convert.ToDouble(txtCantMat.Text) * Convert.ToDouble(txtPrecioUnitario.Text);
            txtTotalCol.Text = total_col.ToString();
        }

        /// <summary>
        /// Handles the Click event of the btnGuardar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnGuardar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {

                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                WSGestion.MOD_Detalle_No_Estructural DetalleNoEstructural = new WSGestion.MOD_Detalle_No_Estructural();
                DetalleNoEstructural.DETORDNOEST_codorden=codigo_orden;
                DetalleNoEstructural.DETORDNOEST_codmatnoest=codigo_material;
                DetalleNoEstructural.DETORDNOEST_descripcion = txtDesc.Text;
                DetalleNoEstructural.DETORDNOEST_preciomatnoest = Convert.ToDecimal(txtPrecioUnitario.Text);
                DetalleNoEstructural.DETORDNOEST_totallineanoest = Convert.ToDecimal(txtTotalCol.Text);
                DetalleNoEstructural.DETORDNOEST_cantmaterial = Convert.ToInt32(txtCantMat.Text);
                //int cant_usu = wsgest.sel(DetalleEstructural.DETORDEST_codestructura, DetalleEstructural.DETORDEST_codclabsubest, DetalleEstructural.DETORDEST_codsubtipoest, DetalleEstructural.DETORDEST_codmat, codigo_orden);
                //if (cant_usu <= 0)
                //{
                if (wsgest.CRE_DetalleNoEstructural(DetalleNoEstructural))
                    {
                        CargarGrid();
                        MensajeError = "Se ha creado el detalle";
                    }
                    else
                    {
                        MensajeError = "Procedimiento Almacenado no aceptado";
                    }
                //}
                //else
                //{

                //    MensajeError = "El ordenamiento estructural junto con el material ya existen. Intente de nuevo ordenamiento y material diferente.";

                //}






            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Session["Codigo_Orden"] = codigo_orden;
            Session["FormAccion"] = "Editar";
            Response.Redirect("OrdenTrabajo.aspx");

        }
    }
}